package proto_wearebatman;

public class Generator implements IhasOutput {

    private Output output;
    private Wire wire;
    private String name;
    private String data;
    private int actual_bit;

    public Generator(String name) {
        this.output = new Output();
        this.name = name;
        this.data = "0";
        this.actual_bit = -1;

        // Proto Output
        Main.println("Generator created: " + this.name);
    }

    public void operate() {
        if(actual_bit<0) actual_bit = 0;
        char d = (this.data.charAt(this.actual_bit++));
        this.actual_bit %= this.data.length();
        this.output.setData(d == '0' ? 0 : 1);

        //Main.println(this.name + ".operate() | Data = "+this.output.getData());

        this.wire.operate();
    }

    public int getOutputData(int pin) {
        return this.output.getData();
    }

    public void setOutputData(int pin, int data) {
        this.output.setData(data);
    }

    public void connectWireOutput(int pin, Wire wire) {
        this.wire = wire;
    }

    public String getName() {
        return this.name;
    }

    public void setData(String data) {
        this.data = data;

        // Proto Output
        Main.println(this.name + "'s sequence is set to: " + data);
    }

    public void resetCounter() {
    }

    public String getFinalState() {
        String s = "";
        if(this.actual_bit >= 0) {
            int a = this.actual_bit-1;
            a = a<0?this.data.length()-1:a;
            s = " ("
                + this.data.substring(0, a)
                + "{" + this.data.charAt(a) + "}"
                + ((a < this.data.length()-1)?this.data.substring(a+1):"")
                + ")";
        } else {
            s = " ("+this.data+")";
        }
        return Integer.toString(this.output.getData()) + s;
    }
}
